<template>
	<view class="content">
		<navTop></navTop>
		<btmBack></btmBack>
		<view class="qiun-columns">
			<view class="qiun-bg-white qiun-title-bar qiun-common-mt qiun-rows" >
				<view class="qiun-title-dot-light">K线图与柱状图联动</view>
				<view style="flex: 1;qiun-rows;text-align: right;">
					<button type="default" size="mini" @tap="tapButton('in')">放大</button>
					<button type="default" size="mini" style="margin-left: 20upx;" @tap="tapButton('out')">缩小</button>
				</view>
			</view>
			<view class="qiun-charts">
				<!--#ifdef MP-ALIPAY -->
				<canvas canvas-id="canvasCandle" id="canvasCandle" class="charts" :width="cWidth*pixelRatio" :height="cHeight*pixelRatio" :style="{'width':cWidth+'px','height':cHeight+'px'}" disable-scroll=true @touchstart="touchCandle" @touchmove="moveCandle" @touchend="touchEndCandle"></canvas>
				<!--#endif-->
				<!--#ifndef MP-ALIPAY -->
				<canvas canvas-id="canvasCandle" id="canvasCandle" class="charts" disable-scroll=true @touchstart="touchCandle" @touchmove="moveCandle" @touchend="touchEndCandle"></canvas>
				<!--#endif-->
			</view>
			<view class="qiun-charts2" >
				<!--#ifdef MP-ALIPAY -->
				<canvas canvas-id="canvasColumn" id="canvasColumn" class="charts2" :width="cWidth*pixelRatio" :height="cHeight*pixelRatio" :style="{'width':cWidth+'px','height':cHeight+'px'}" ></canvas>
				<!--#endif-->
				<!--#ifndef MP-ALIPAY -->
				<canvas canvas-id="canvasColumn" id="canvasColumn" class="charts2"></canvas>
				<!--#endif-->
			</view>
			<view class="qiun-padding qiun-bg-white ">
				<slider :value="itemCount" min="5" :max="sliderMax" block-color="#f8f8f8" block-size="18" @changing="sliderMove" @change="sliderMove"/>
			</view>
			
			
		</view>
	</view>
</template>

<script>
	import navTop from '@/components/navTop.vue';
	import btmBack from '@/components/btmBack.vue';
	import uCharts from '@/components/u-charts/u-charts.js';
	import day from '@/jsondata/stock/day.js';

	var _self;
	var canvaCandle=null;
	var canvaColumn=null;

	export default {
		components: {
			navTop,
			btmBack,
		},
		data() {
			return {
				cWidth:'',
				cHeight:'',
				cHeight2:'',
				pixelRatio:1,
				itemCount:20,//x轴单屏数据密度
				sliderMax:50
			}
		},
		onLoad() {
			_self = this;
			//#ifdef MP-ALIPAY
			uni.getSystemInfo({
				success: function (res) {
					if(res.pixelRatio>1){
						//正常这里给2就行，如果pixelRatio=3性能会降低一点
						//_self.pixelRatio =res.pixelRatio;
						_self.pixelRatio =2;
					}
				}
			});
			//#endif
			this.cWidth=uni.upx2px(750);
			this.cHeight=uni.upx2px(500);
			this.cHeight2=uni.upx2px(200);
			this.getServerData();
		},
		methods: {
			async getServerData() {

				// uni.request({
				// 	url: 'https://www.ucharts.cn/data.json',
				// 	data:{
				// 	},
				// 	success: function(res) {
				//let res = await this.$api.json('res');
				let res = await day.res;

				let Candle={categories:[],series:[]};
				let Column={categories:[],series:[]};
				//这里我后台返回的是数组，所以用等于，如果您后台返回的是单条数据，需要push进去
				Candle.categories=res.data.Candle.categories;
				Candle.series=res.data.Candle.series;
				Column.categories=res.data.CandleColumn.categories;
				Column.series=res.data.CandleColumn.series;
				_self.showCandle("canvasCandle",Candle);
				_self.showColumn("canvasColumn",Column);
				// 	},
				// 	fail: () => {
				// 		_self.tips="网络错误，小程序端请检查合法域名";
				// 	},
				// });
			},
			showCandle(canvasId,chartData){
					canvaCandle=new uCharts({
						$this:_self,
						canvasId: canvasId,
						type: 'candle',
						fontSize:11,
						padding:[10,15,0,15],
						legend:{
							show:true,
						},
						background:'#FFFFFF',
						pixelRatio:_self.pixelRatio,
						categories: chartData.categories,
						series: chartData.series,
						animation: false,
						enableScroll: true,
						xAxis: {
							disableGrid:true,
							labelCount:4,
							//type:'grid',
							//gridType:'dash',
							itemCount:_self.itemCount,
							scrollShow:true,
							scrollAlign:'right',
						},
						yAxis: {
							//disabled:true
							gridType:'dash',
							splitNumber:5,
							format:(val)=>{return val.toFixed(0)}
						},
						width: _self.cWidth*_self.pixelRatio,
						height: _self.cHeight*_self.pixelRatio,
						dataLabel: false,
						dataPointShape: true,
						extra: {
							candle:{
								color:{
									upLine:'#f04864',
									upFill:'#f04864',
									downLine:'#2fc25b',
									downFill:'#2fc25b'
								},
								average:{
									show:true,
									name:['MA5','MA10','MA30'],
									day:[5,10,20],
									color:['#1890ff', '#2fc25b', '#facc14']
								}
							},
							tooltip:{
								bgColor:'#000000',
								bgOpacity:0.7,
								gridType:'dash',
								dashLength:5,
								gridColor:'#1890ff',
								fontColor:'#FFFFFF',
								horizentalLine:true,
								xAxisLabel:true,
								yAxisLabel:true,
								labelBgColor:'#DFE8FF',
								labelBgOpacity:0.95,
								labelAlign:'left',
								labelFontColor:'#666666'
							}
						},
					});
					
				},
				touchCandle(e){
					canvaCandle.scrollStart(e);
				},
				moveCandle(e) {
					let distance = canvaCandle.scroll(e);
					if(distance!==undefined){
						canvaColumn.translate(distance);
					}
				},
				touchEndCandle(e) {
					canvaCandle.scrollEnd(e);
					//下面是toolTip事件，如果滚动后不需要显示，可不填写
					canvaCandle.showToolTip(e, {
						format: function (item, category) {
							return category + ' ' + item.name + ':' + item.data 
						}
					});
				},
				tapButton(type){
					let step=5;
					if(type=='in'){
						_self.itemCount -= step;
						if(_self.itemCount<=5){
							_self.itemCount=5;
						}
					}else{
						_self.itemCount += step;
						if(_self.itemCount>=_self.sliderMax){
							_self.itemCount=_self.sliderMax;
						}
					}
					_self.zoomCandle(_self.itemCount);
				},
				sliderMove(e){
					_self.itemCount=e.detail.value;
					_self.zoomCandle(e.detail.value);
				},
				zoomCandle(val) {
					canvaCandle.zoom({
						itemCount: val
					});
					canvaColumn.zoom({
						itemCount: val
					});
				},
				showColumn(canvasId,chartData){
					canvaColumn=new uCharts({
						$this:_self,
						canvasId: canvasId,
						type: 'column',
						padding:[10,15,0,15],
						legend:{
							show:false,
						},
						fontSize:11,
						background:'#FFFFFF',
						pixelRatio:_self.pixelRatio,
						animation: false,
						enableScroll:true,
						dataLabel:false,
						categories: chartData.categories,
						series: chartData.series,
						xAxis: {
							disabled:true,
							disableGrid:true,
							labelCount:4,
							itemCount:_self.itemCount,
							scrollAlign:'right',
						},
						yAxis: {
							disableGrid:true,
							splitNumber: 2,
							min:0,
							format:(val)=>{return val.toFixed(0)}
						},
						width: _self.cWidth*_self.pixelRatio,
						height: _self.cHeight2*_self.pixelRatio,
						extra: {
							column: {
								type:'group',
							}
						  }
					});
					
				}
			

		}

	}
</script>

<style lang="scss">
	page,
	.content {

		width: 100%;
		height: 100%;
		overflow: hidden;

		background-color: $page-color-base;
	}
	/*样式的width和height一定要与定义的cWidth和cHeight相对应*/
	.qiun-charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
	
	.charts {
		width: 750upx;
		height: 500upx;
		background-color: #FFFFFF;
	}
	.qiun-charts2 {
		width: 750upx;
		height: 200upx;
		background-color: #FFFFFF;
	}
	
	.charts2 {
		width: 750upx;
		height: 200upx;
		background-color: #FFFFFF;
	}
</style>
